import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class;

/**
 * MessageTabPane
 * @date 2019-06-13
 */
import React, { Component } from 'react';
import { Bind } from 'lodash-decorators';
import { Badge, Icon, Radio, Table } from 'antd';
import { tableScrollWidth } from 'utils/utils';
import { dateTimeRender } from 'utils/renderer';
import intl from 'utils/intl';
import { DEFAULT_DATETIME_FORMAT } from 'utils/constants';
import FilterForm from "./FilterForm";

function getRefFieldsValue(ref) {
  if (ref.current) {
    return ref.current.props.form.getFieldsValue();
  }

  return {};
}

var MessageTabPane = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), (_class = /*#__PURE__*/function (_Component) {
  _inherits(MessageTabPane, _Component);

  var _super = _createSuper(MessageTabPane);

  function MessageTabPane(props) {
    var _this;

    _classCallCheck(this, MessageTabPane);

    _this = _super.call(this, props);
    _this.state = {
      // prevDataSource: [], 之前的默认值
      // eslint-disable-next-line react/no-unused-state
      selectedRows: [],
      selectedRowKeys: [],
      readTypeValue: 'all',
      expand: false
    };
    _this.filterFormRef = React.createRef();
    return _this;
  }

  _createClass(MessageTabPane, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props$paginatio = this.props.pagination,
          pagination = _this$props$paginatio === void 0 ? {} : _this$props$paginatio;
      this.handleFetch({
        page: pagination
      });
    } // 加载数据

  }, {
    key: "handleFetch",
    value: function handleFetch() {
      var _this2 = this;

      var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          fetchMessage = _this$props.fetchMessage,
          type = _this$props.type;
      var readTypeValue = this.state.readTypeValue;
      var fieldsValue = getRefFieldsValue(this.filterFormRef);
      fieldsValue.fromDate = fieldsValue.fromDate && fieldsValue.fromDate.format(DEFAULT_DATETIME_FORMAT);
      fieldsValue.toDate = fieldsValue.toDate && fieldsValue.toDate.format(DEFAULT_DATETIME_FORMAT);

      var allPayload = _objectSpread(_objectSpread({}, payload), fieldsValue);

      switch (readTypeValue) {
        case '0':
          allPayload.readFlag = 0;
          break;

        case '1':
          allPayload.readFlag = 1;
          break;

        case 'all':
        default:
          break;
      }

      fetchMessage(allPayload, type).then(function (res) {
        if (res) {
          var _this2$props$selected = _this2.props.selectedRows,
              prevSelectedRows = _this2$props$selected === void 0 ? [] : _this2$props$selected;

          _this2.setState({
            // eslint-disable-next-line react/no-unused-state
            selectedRows: [],
            selectedRowKeys: []
          }, function () {
            // 当 因为加载数据 导致 选中数据变化时, 父组件需要强制刷新
            if (type !== 'announce') {
              if (prevSelectedRows.length !== 0) {
                var indexForceUpdate = _this2.props.indexForceUpdate;
                indexForceUpdate();
              }
            }
          });
        }
      });
    }
  }, {
    key: "handleFormSearch",
    value: function handleFormSearch() {
      var fieldsValue = getRefFieldsValue(this.filterFormRef);
      fieldsValue.fromDate = fieldsValue.fromDate && fieldsValue.fromDate.format(DEFAULT_DATETIME_FORMAT);
      fieldsValue.toDate = fieldsValue.toDate && fieldsValue.toDate.format(DEFAULT_DATETIME_FORMAT);
      this.handleFetch(fieldsValue);
    } // Table

  }, {
    key: "getColumns",
    value: function getColumns() {
      var _this3 = this;

      var _this$props$type = this.props.type,
          type = _this$props$type === void 0 ? 'message' : _this$props$type;

      if (type === 'announce') {
        return [{
          title: intl.get('cmsg.userMessage.model.userMessage.title').d('标题内容'),
          dataIndex: 'title'
        }, {
          title: intl.get('cmsg.userMessage.model.userMessage.creationDate').d('提交时间'),
          width: 200,
          dataIndex: 'publishedDate',
          render: dateTimeRender
        }, {
          title: intl.get('cmsg.common.view.type').d('类型'),
          width: 200,
          dataIndex: 'receiverTypeMeaning'
        }];
      }

      return [{
        title: intl.get('cmsg.userMessage.model.userMessage.title').d('标题内容'),
        dataIndex: 'subject',
        render: function render(text, record) {
          if (record.readFlag === 0) {
            return /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(Badge, {
              status: "processing"
            }), text);
          } else {
            return /*#__PURE__*/React.createElement("span", {
              style: {
                color: '#999'
              }
            }, /*#__PURE__*/React.createElement(Badge, {
              status: "default"
            }), text);
          }
        }
      }, {
        title: intl.get('cmsg.userMessage.model.userMessage.creationDate').d('提交时间'),
        width: 200,
        dataIndex: 'creationDate',
        render: function render(text, record) {
          if (record.readFlag === 0) {
            return dateTimeRender(text);
          } else {
            return /*#__PURE__*/React.createElement("span", {
              style: {
                color: '#999'
              }
            }, dateTimeRender(text));
          }
        }
      }, {
        title: intl.get('cmsg.common.view.type').d('类型'),
        width: 200,
        dataIndex: 'messageTypeCode',
        render: function render(text, record) {
          return _this3.typeCodeRender(record);
        }
      }];
    }
  }, {
    key: "handleRowSelectionChange",
    value: function handleRowSelectionChange(_) {
      var _this4 = this;

      var selectedRows = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
      this.setState({
        // eslint-disable-next-line react/no-unused-state
        selectedRows: selectedRows,
        selectedRowKeys: selectedRows.map(function (r) {
          return r.userMessageId;
        })
      }, function () {
        var _this4$props = _this4.props,
            type = _this4$props.type,
            indexForceUpdate = _this4$props.indexForceUpdate;

        if (type !== 'announce') {
          indexForceUpdate();
        }
      });
    }
  }, {
    key: "handleTableChange",
    value: function handleTableChange(page, filter, sort) {
      var params = {
        page: page,
        sort: sort
      };
      this.handleFetch(params);
    }
  }, {
    key: "handleDetails",
    value: function handleDetails(record) {
      var _this$props2 = this.props,
          onGotoDetail = _this$props2.onGotoDetail,
          type = _this$props2.type;
      onGotoDetail(record, type);
    }
    /**
     * 全部消息 已读消息 未读消息 切换
     * @param e
     */

  }, {
    key: "handleType",
    value: function handleType(e) {
      var _this5 = this;

      this.setState({
        readTypeValue: e.target.value
      }, function () {
        _this5.handleFetch();
      });
    }
  }, {
    key: "handleUp",
    value: function handleUp() {
      this.setState({
        expand: false
      });
    }
  }, {
    key: "handleDown",
    value: function handleDown() {
      this.setState({
        expand: true
      });
    }
    /**
     * 处理站内消息类型
     */

  }, {
    key: "typeCodeRender",
    value: function typeCodeRender() {
      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      // const { messageCategoryMeaning, messageSubcategoryMeaning } = record;
      var userMessageTypeMeaning = record.userMessageTypeMeaning; // const array = [messageCategoryMeaning, messageSubcategoryMeaning].filter(o => o);

      if (record.readFlag === 0) {
        // return array.join('-');
        return userMessageTypeMeaning;
      } else {
        // return <span style={{ color: '#999' }}>{array.join('-')}</span>;
        return /*#__PURE__*/React.createElement("span", {
          style: {
            color: '#999'
          }
        }, userMessageTypeMeaning);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this6 = this;

      var _this$props3 = this.props,
          _this$props3$dataSour = _this$props3.dataSource,
          dataSource = _this$props3$dataSour === void 0 ? [] : _this$props3$dataSour,
          _this$props3$paginati = _this$props3.pagination,
          pagination = _this$props3$paginati === void 0 ? false : _this$props3$paginati,
          _this$props3$loading = _this$props3.loading,
          loading = _this$props3$loading === void 0 ? false : _this$props3$loading,
          _this$props3$type = _this$props3.type,
          type = _this$props3$type === void 0 ? 'message' : _this$props3$type;
      var _this$state = this.state,
          _this$state$selectedR = _this$state.selectedRowKeys,
          selectedRowKeys = _this$state$selectedR === void 0 ? [] : _this$state$selectedR,
          readTypeValue = _this$state.readTypeValue,
          expand = _this$state.expand;
      var columns = this.getColumns();
      var rowSelection = type === 'announce' ? null : {
        selectedRowKeys: selectedRowKeys,
        onChange: this.handleRowSelectionChange
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, type !== 'announce' && /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(Radio.Group, {
        buttonStyle: "solid",
        onChange: this.handleType
      }, /*#__PURE__*/React.createElement(Radio.Button, {
        value: "all",
        checked: readTypeValue === 'all'
      }, intl.get('cmsg.userMessage.view.option.allMessage').d('全部消息')), /*#__PURE__*/React.createElement(Radio.Button, {
        value: "0",
        checked: readTypeValue === '0'
      }, intl.get('cmsg.userMessage.view.option.unReadMessage').d('未读消息')), /*#__PURE__*/React.createElement(Radio.Button, {
        value: "1",
        checked: readTypeValue === '1'
      }, intl.get('cmsg.userMessage.view.option.readMessage').d('已读消息'))), /*#__PURE__*/React.createElement("a", {
        onClick: expand ? this.handleUp : this.handleDown,
        style: {
          color: '#00CCFF',
          marginLeft: 8,
          cursor: 'pointer'
        }
      }, /*#__PURE__*/React.createElement("span", null, expand ? intl.get("cpaas.common.button.up").d('收起') : intl.get("cpaas.common.button.expand").d('展开')), /*#__PURE__*/React.createElement(Icon, {
        type: expand ? 'up' : 'down',
        style: {
          fontSize: 16
        }
      }))), (expand || type === 'announce') && /*#__PURE__*/React.createElement(FilterForm, {
        wrappedComponentRef: this.filterFormRef,
        onSearch: this.handleFormSearch
      }), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: type === 'announce' ? 'noticeId' : 'userMessageId',
        style: {
          cursor: 'pointer'
        },
        loading: loading,
        onRow: function onRow(record) {
          return {
            onClick: function onClick() {
              _this6.handleDetails(record);
            }
          };
        },
        rowSelection: rowSelection,
        dataSource: dataSource,
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        pagination: pagination,
        onChange: this.handleTableChange
      }));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps, prevState) {
      var dataSource = nextProps.dataSource;
      var prevDataSource = prevState.prevDataSource;

      if (dataSource !== prevDataSource) {
        return {
          prevDataSource: dataSource,
          selectedRows: [],
          selectedRowKeys: []
        };
      }

      return null;
    }
  }]);

  return MessageTabPane;
}(Component), (_applyDecoratedDescriptor(_class.prototype, "handleFormSearch", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleFormSearch"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRowSelectionChange", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleRowSelectionChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleTableChange", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "handleTableChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleDetails", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "handleDetails"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleType", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "handleType"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleUp", [_dec6], Object.getOwnPropertyDescriptor(_class.prototype, "handleUp"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleDown", [_dec7], Object.getOwnPropertyDescriptor(_class.prototype, "handleDown"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "typeCodeRender", [_dec8], Object.getOwnPropertyDescriptor(_class.prototype, "typeCodeRender"), _class.prototype)), _class));
export { MessageTabPane as default };